﻿using ToolKitManagerIESiteList.Util;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace ToolKitManagerIESiteList
{
    public partial class FrmEditFilterDomains: Form
    {
        public FrmEditFilterDomains()
        {
            InitializeComponent();
        }

        private void FrmEditFilterDomains_Load(object sender, EventArgs e)
        {
            ListItems();
        }

        // Add an item value.
        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtValue.Text = txtValue.Text.Trim();
            if (Properties.Settings.Default.ExcludeDomains.Contains(txtValue.Text))
            {
                this.ShowWarning(Properties.Resources.DomainAlreadyExists, AssemblyHelper.AssemblyTitle);
                txtValue.Clear();
                txtValue.Focus();
                return;
            }
            Properties.Settings.Default.ExcludeDomains.Add(txtValue.Text);
            txtValue.Clear();
            txtValue.Focus();
            ListItems();
        }

        // Remove an item value.
        private void btnRemove_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.ExcludeDomains.Remove(txtValue.Text);
            txtValue.Clear();
            txtValue.Focus();
            ListItems();
        }

        // List the current items.
        private void ListItems()
        {
            lstItems.DataSource =
                Properties.Settings.Default.ExcludeDomains.Cast<string>().OrderBy(i => i).ToArray();
            lstItems.SelectedIndex = -1;
        }

        // Save settings.
        private void FrmEditFilterDomains_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.None)
            {
                e.Cancel = true;
            }
            else
            {
                Properties.Settings.Default.Save();
            }
        }

        // Display the selected item.
        private void lstItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstItems.SelectedItem == null)
                txtValue.Clear();
            else
                txtValue.Text = lstItems.SelectedItem.ToString();
        }

        private void BntClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
